const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // compaynArr:[{id:1,name:'公司一',},{id:2,name:'公司二',}],
    arr_sex:[{id:1,name:'全职',},{id:2,name:'兼职',}],
    tindex:null,//选中的id
    compayindex:0,//选中的id
    compayguimo:[],//公司规模
    xlindex:0,
    guimo:'',///上传的规模
    NatureArr:[],//企业性质
    xzindex:0,
    xingzhi:'',///上传企业性质
    hangye:'选择行业',
    zhiwei:'选择职位',
    startTime:'',//开始时间
    endTime:'',///结束时间
    workType:'',//工作类型  全职 兼职
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    var date = that.formatTime(new Date);
    that.setData({
      dateValue_d_v:date,
      id:options.id 
    });
    // 公司规模
    wx.request({
      url: url + 'dict/getEnterpriseSize',
      method: "GET",
      data:{},
      success:function(res){
        console.log("公司规模",res);
        var ret = res.data
          that.setData({
            xueliArr:ret.data,//公司名称
          })
        
      }
    })
    //企业性质
    wx.request({
      url: url + 'dict/getEnterpriseNature',
      method: "GET",
      data:{},
      success:function(res){
        console.log("企业性质",res);
        var ret = res.data
          that.setData({
            NatureArr:ret.data,//公司名称
          })
        
      }
    })
    if(options.id > 0){
      wx.request({
        url: url + 'cvExperience/getCVExperienceById',
        method: "GET",
        data:{id:options.id },
        success:function(res){
          console.log("根据工作经历ID获取简历基本信息详情",res);
          var ret = res.data
          if(ret.code == 200){
            that.setData({jldetail:ret.data,
              compayname:ret.data.companyName,//公司名称
              hangye:ret.data.industry,//所属行业
              guimo:ret.data.companySize,//公司规模
              createTime:'',//    创建时间
              bumen:ret.data.department,//部门
              zhiwei:ret.data.position,//职位
              endTime:ret.data.endTime,//结束时间
              xingzhi:ret.data.nature,//公司性质
              startTime:ret.data.startTime,//开始时间
              updateTime:'',//更新时间
              des:ret.data.workDescribe,//工作描述
              workType:'',//
            })
          }else{
            that.setData({jldetail:''})
            wx.showToast({
              title: ret.msg,
              icon:'none'
            })
          }
        }
      })
    }else{
      console.log('=0')
      return false
    }
  },
  // 删除经历
  del(){
    var that = this
    var id = that.data.id
    wx.request({
      url: url + 'cvExperience/delete?id='+id,
      method: "DELETE",
      // data:{id:id },
      success:function(res){
        console.log("删除经历",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title:'删除成功',
            icon:'none'
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          
          that.setData({sub:true})
          wx.showToast({
            title: '删除失败',
            icon:'none'
          })
        }
      }
    })
  },
  // 公司名称
  compayname(e){
    var that = this
    that.setData({compayname:e.detail.value})
  },

  onShow(){
    var that = this
    var hangye = wx.getStorageSync('hangye')
    var zhiwei = wx.getStorageSync('zhiwei')
    if(hangye){
      that.setData({hangye:hangye})
    }
    if(zhiwei){
      that.setData({zhiwei:zhiwei})
    }
      
  },
  // 所属行业
  hangye(e){
    var that = this
    // that.setData({hangye:e.detail.value})
    wx.navigateTo({
      url: '/HomePage/industry/industry',
    })
  },
  //工作类型
  xingbieChange: function (e) {
    var that = this;
    var val = e.detail.value
    if(val == 0){
      var workType = 1
    }else{
      var workType = 2
    }
    console.log('sex index------',workType)
    that.setData({tindex:val,workType:workType})
  },
  // 公司规模
  xinziChange: function (e) {
    var that = this;
    var val = e.detail.value//这个是  index
    var skill = that.data.xueliArr
    for(let item in skill){
      if(item == val){
        that.setData({guimo:skill[item].subTypeName,})
        // that.setData({level:skill[item].id,})
        break;
      }
    }
    console.log(val)
    that.setData({xlindex:val,})//页面赋值 title:name
  },

  // tiaozhuan(e){
  //   var that = this
  //   wx.navigateTo({
  //     url: '/HomePage/zwlist/zwlist',
  //   })
  // },
  zhiwei(e){
    var that = this
    that.setData({zhiwei:e.detail.value})
  },
  bumen(e){
    var that = this
    that.setData({bumen:e.detail.value})
  },
  //性质
  // xingzhi(e){
  //   var that = this
  //   that.setData({xingzhi:e.detail.value})
  // },
  qiyeChange: function (e) {
    var that = this;
    var val = e.detail.value//这个是  index
    var NatureArr = that.data.NatureArr
    for(let item in NatureArr){
      if(item == val){
        that.setData({xingzhi:NatureArr[item].subTypeName,})
        break;
      }
    }
    console.log(val)
    that.setData({xzindex:val,})//页面赋值 title:name
  },

  // startTime(e){
  //   var that = this
  //   that.setData({startTime:e.detail.value})
  // },
  bindDateChange: function(e) {
    console.log('startTime', e.detail.value)
    this.setData({
      // date: e.detail.value,
      startTime: e.detail.value
    })
  },
  // endTime(e){
  //   var that = this
  //   that.setData({endTime:e.detail.value})
  // },
  jieshuChange: function(e) {
    console.log('endTime', e.detail.value)
    this.setData({
      // date: e.detail.value,
      endTime: e.detail.value
    })
  },
  
  des(e){
    var that = this
    that.setData({des:e.detail.value})
  },




  datePickerBindchange:function(e){
    this.setData({
      dateValue_s:e.detail.value,
      dateValue_s_v:e.detail.value
    })
  },
  datePickerBindchangeend:function(e){
    this.setData({
      dateValue_e:e.detail.value,
      dateValue_e_v:e.detail.value
    })
  },
  formatTime:function(date){
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    if(month < 10){
      month = '0'+month;
    }
    if(day < 10){
      day = '0'+day;
    }
    return year+'-'+month+'-'+day;
  },
  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  savedata(e){
    var that = this
    var jiliid = that.data.id
    if(jiliid > 0){
      //编辑
      var method = 'PUT'
      var api = 'cvExperience/updateExperience'
      var jiliid = jiliid
    }else{
      // 添加
      var method = 'POST'
      var api = 'cvExperience/add'
      var jiliid = 0
    }
    var uid = wx.getStorageSync('user_massage')


    if(that.data.compayname == '' || that.data.compayname == undefined){
      wx.showToast({
        title: '请填写公司名称',
        icon:'none',
      })
      return false
    }
    if(that.data.guimo == ''){
      wx.showToast({
        title: '请选择公司规模',
        icon:'none',
      })
      return false
    }
    if(that.data.bumen == '' || that.data.bumen == undefined){
      wx.showToast({
        title: '请填写部门',
        icon:'none',
      })
      return false
    }
    if(that.data.startTime == '' || that.data.endTime == ''){
      wx.showToast({
        title: '请选择时间',
        icon:'none',
      })
      return false
    }
    if(that.data.hangye == '' || that.data.hangye == '选择行业'){
      wx.showToast({
        title: '请填所属行业',
        icon:'none',
      })
      return false
    }
    if(that.data.xingzhi == ''){
      wx.showToast({
        title: '请选择公司性质',
        icon:'none',
      })
      return false
    }
    if(that.data.zhiwei == ''  || that.data.zhiwei == '选择职位'){
      wx.showToast({
        title: '请输入职位',
        icon:'none',
      })
      return false
    }
    if(that.data.workType == ''){
      wx.showToast({
        title: '请选择工作类型',
        icon:'none',
      })
      return false
    }
    if(that.data.des == '' || that.data.des == undefined){
      wx.showToast({
        title: '请输入工作描述',
        icon:'none',
      })
      return false
    }

    let sensWords = Object.values(this.sensWords || {})
    if(sensWords.length > 0){
      wx.showToast({
        title: `${sensWords.join(',')}包含敏感词，请修改后再${that.data.sttsname}`,
        icon:'none',
      })
			return false;
    }

    var data = {
      approval: '1',
      companyName:that.data.compayname,//公司名称
      companySize:that.data.guimo,//公司规模
      createTime:'',//    创建时间
      department:that.data.bumen,//部门
      startTime:that.data.startTime,//开始时间
      endTime:that.data.endTime,//结束时间
      id:jiliid,//工作经历ID
      industry:that.data.hangye,//所属行业
      nature:that.data.xingzhi,//公司性质
      position:that.data.zhiwei,//职位
      updateTime:'',//更新时间
      userUid:uid.userUid,//
      workDescribe:that.data.des,//工作描述
      workType:that.data.workType,
    }
    wx.request({
      url:url + api,
      header:{
      'context-type': 'application/json',
      },
      method: method,
      data:data,
      success:function(res){
        var ret = res.data
        // console.log(res,'412342123421')
        wx.removeStorageSync('hangye')//选择选中行业的缓存
        if(ret.code == 200){
            wx.showToast({
              title: '保存成功',
              icon:'none',
            })
            setTimeout(function(){
              wx.navigateBack({
                delta: 1,
              })
              // wx.redirectTo({
              //   url:'/HomePage/linejianli/linejianli'
              // })
            },1000)
        }else{
          wx.showToast({
            title: '保存失败',
            icon:'none',
          })
        }
      }
    })


    // wx.redirectTo({
    //      url:'/HomePage/linejianli/linejianli'
    // })
  }
})